home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / slideshow / sources (complete) / controller.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  6.6 KB  |  242 lines

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. public class Controller extends Window
  6. {
  7.     //Declare constants
  8.     //Insert "Controller Constants"
  9.     public static final String BACKWARD_COMMAND = "backward";
  10.     public static final String FORWARD_COMMAND = "forward";
  11.     public static final String PLAY_COMMAND = "play";
  12.     public static final String PAUSE_COMMAND = "pause";
  13.     
  14.     protected static final String imagePath = "images/controlborder.jpg";
  15.     protected static final int collapsedWidth = 9;
  16.  
  17.     //Declare data members
  18.     //Insert "Controller data members"
  19.     protected Image image;
  20.     protected Frame frame;
  21.     
  22.     protected BackwardButton backwardButton1;
  23.     protected PlayPauseButton playPauseButton1;
  24.     protected ForwardButton forwardButton1;
  25.     protected CloseBoxButton closeBoxButton1;
  26.     protected DumbContainer container;
  27.  
  28.     protected ActionListener actionListener = null;
  29.  
  30.     /**
  31.      * Creates a Controller object with the specified parent Frame.
  32.      * @param the parent frame to associate the controller with.
  33.      */
  34.     public Controller(Frame parent)
  35.     {
  36.         //Setup parent info for this window
  37.         //Insert "Controller parent info"
  38.         super(parent);
  39.         frame = parent;
  40.         
  41.         //INIT_CONTROLS
  42.         //Setup and layout objects in the controller
  43.         //Insert "Controller init controls"
  44.         setVisible(false);
  45.         setLayout(null);
  46.         setSize(90,30);
  47.         
  48.         closeBoxButton1 = new CloseBoxButton();
  49.         closeBoxButton1.setLocation(1,1);
  50.         closeBoxButton1.setSize(closeBoxButton1.getPreferredSize());
  51.         add(closeBoxButton1);
  52.         
  53.         container = new DumbContainer();
  54.         container.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
  55.         container.setBounds(9, 1, 81, 28);
  56.         
  57.         backwardButton1 = new BackwardButton();
  58.         backwardButton1.setBounds(10,1,20,40);
  59.         container.add(backwardButton1);
  60.         
  61.         playPauseButton1 = new PlayPauseButton();
  62.         playPauseButton1.setBounds(0,0,20,40);
  63.         container.add(playPauseButton1);
  64.         
  65.         forwardButton1 = new ForwardButton();
  66.         forwardButton1.setBounds(0,0,20,40);
  67.         container.add(forwardButton1);
  68.         
  69.         add(container);
  70.     
  71.         //REGISTER_LISTENERS
  72.         //Register our action listener with our buttons
  73.         //Insert "Controller register listeners"
  74.         Action aAction = new Action();
  75.         backwardButton1.addActionListener(aAction);
  76.         forwardButton1.addActionListener(aAction);
  77.         playPauseButton1.addActionListener(aAction);
  78.         closeBoxButton1.addActionListener(aAction);
  79.         
  80.         //Initialize state information.
  81.         //Insert "Controller init state"
  82.         image = Misc.loadImage(imagePath, parent, true);
  83.         
  84.         setSize(getPreferredSize());
  85.         //Work around a MRJ Bug.
  86.         setLocation(-5,-21);
  87.     }
  88.     
  89.     /**
  90.      * Set the state of the Play/Pause button
  91.      * @param if true, the button will be in the Play state;
  92.      * If false it will be in the Pause state.
  93.      * @see #isPlayState
  94.      */
  95.     public void setPlayState(boolean isPlay)
  96.     {
  97.         //Handle setup for the appropriate state
  98.         //Insert "Controller setPlayState"
  99.         if (isPlay)
  100.             playPauseButton1.setState(PlayPauseButton.PLAY_STATE);
  101.         else
  102.             playPauseButton1.setState(PlayPauseButton.PAUSE_STATE);
  103.     }
  104.     
  105.     /**
  106.      * Get the current state of the Play/Pause button
  107.      * @return true if the button is in the Play state,
  108.      * false if it is in the Pause state.
  109.      * @see #setPlayState
  110.      */
  111.     public boolean isPlayState()
  112.     {
  113.         //Return the current state
  114.         //Insert "Controller isPlayState"
  115.         return playPauseButton1.getState() == PlayPauseButton.PLAY_STATE;
  116.     }
  117.     
  118.     //Routines for handling ActionListener management.
  119.     //Insert "Controller Action Management"
  120.     /**
  121.      * Adds the specified action listener to receive action events.
  122.      * @param l the action listener
  123.      */
  124.     public void addActionListener(ActionListener l)
  125.     {
  126.         actionListener = AWTEventMulticaster.add(actionListener, l);
  127.     }
  128.  
  129.     /**
  130.      * Removes the specified action listener so it no longer receives
  131.      * action events from this component.
  132.      * @param l the action listener
  133.      */
  134.     public void removeActionListener(ActionListener l)
  135.     {
  136.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  137.     }
  138.     
  139.     /**
  140.      * Fire an action event to the listeners.
  141.      * @param command, the command String to send with the ActionEvent
  142.      */
  143.     protected void fireActionEvent(String command)
  144.     {
  145.         if (actionListener != null)
  146.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, command));
  147.     }
  148.  
  149.     public void paint(Graphics g)
  150.     {
  151.         //Handle painting the border image, and let the super class paint the rest.
  152.         //Insert "Controller paint"
  153.         if (image != null)
  154.             g.drawImage(image, 0, 0, this);
  155.         super.paint(g);
  156.     }
  157.     
  158.     public void update(Graphics g)
  159.     {
  160.         //Override update to simply call paint to reduce flicker.
  161.         //Insert "Controller update"
  162.         paint(g);
  163.     }
  164.     
  165.     /**
  166.      * Gets the size the controller should be to look its best
  167.      * @return the dimensions the controller renders its self the best.
  168.      */
  169.     public Dimension getPreferredSize()
  170.     {
  171.         //If the current image is not null, then return the size of the image.
  172.         //If it is null, defer to the super class.
  173.         //Insert "Controller getPreferredSize"
  174.         if (image != null)
  175.             return new Dimension (image.getWidth(frame), image.getHeight(frame));
  176.  
  177.         return super.getPreferredSize();
  178.     }
  179.  
  180.     //Inner class so we can instantiate a simple lightweight container
  181.     //for use in laying out the controller properly.
  182.     class DumbContainer extends Container { }
  183.     
  184.     //Inner class to handle action events
  185.     //Insert "Controller Action"
  186.     class Action implements ActionListener
  187.     {
  188.         public void actionPerformed(ActionEvent event)
  189.         {
  190.             Object object = event.getSource();
  191.             if (object == backwardButton1)
  192.                 backwardButton1_ActionPerformed(event);
  193.             else if (object == forwardButton1)
  194.                 forwardButton1_ActionPerformed(event);
  195.             else if (object == playPauseButton1)
  196.                 playPauseButton1_ActionPerformed(event);
  197.             else if (object == closeBoxButton1)
  198.                 closeBoxButton1_ActionPerformed(event);
  199.                 
  200.         }
  201.     }
  202.  
  203.     //Routines to handle action events from the different buttons
  204.     //Insert "Controller button actions"
  205.     void backwardButton1_ActionPerformed(ActionEvent event)
  206.     {
  207.         fireActionEvent(BACKWARD_COMMAND);
  208.     }
  209.  
  210.     void forwardButton1_ActionPerformed(ActionEvent event)
  211.     {
  212.         fireActionEvent(FORWARD_COMMAND);
  213.     }
  214.  
  215.     void playPauseButton1_ActionPerformed(ActionEvent event)
  216.     {
  217.         String command = event.getActionCommand();
  218.         try
  219.         {
  220.             int state = Integer.valueOf(command).intValue();
  221.             switch (state)
  222.             {
  223.                 case PlayPauseButton.PLAY_STATE:
  224.                     fireActionEvent(PLAY_COMMAND);
  225.                     playPauseButton1.setState(PlayPauseButton.PAUSE_STATE);
  226.                     break;
  227.                 case PlayPauseButton.PAUSE_STATE:
  228.                     fireActionEvent(PAUSE_COMMAND);
  229.                     playPauseButton1.setState(PlayPauseButton.PLAY_STATE);
  230.                     break;
  231.             }
  232.         }
  233.         catch(NumberFormatException exc) { }
  234.     }
  235.  
  236.     void closeBoxButton1_ActionPerformed(ActionEvent event)
  237.     {
  238.         setVisible(false);
  239.         dispose();
  240.     }
  241. }
  242.